﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Collections;

/// <summary>
///HouseDao 的摘要说明
/// </summary>
public class HouseDao
{
    DBMM dbm = new DBMM();
    DataTable dt = null;
    string sql = null;
    HouseVo hv = new HouseVo();

    //添加新楼盘
    public Exception addHouse(HouseVo hv)
    {
        sql = "insert into House values(@Hname,@Hintroduce,@Hcontent,@Haddress,@Hcost,@Harea,@Htime,@Htel,@Hperson,@Hcenter,@Hdeveloper,@Hcompany,@Hlogo,@Hpic,@Hmap,@Htype,@Hclass,0,0)";
        SqlParameter[] sp = new SqlParameter[] { new SqlParameter("@Hname", hv.Hname), 
                                                 new SqlParameter("@Hintroduce", hv.Hintroduce), 
                                                 new SqlParameter("@Hcontent", hv.Hcontent), 
                                                 new SqlParameter("@Haddress", hv.Haddress), 
                                                 new SqlParameter("@Hcost", hv.Hcost),
                                                 new SqlParameter("@Harea", hv.Harea),
                                                 new SqlParameter("@Htime", hv.Htime),
                                                 new SqlParameter("@Htel", hv.Htel),
                                                 new SqlParameter("@Hperson", hv.Hperson),
                                                 new SqlParameter("@Hcenter", hv.Hcenter),
                                                 new SqlParameter("@Hdeveloper", hv.Hdeveloper),
                                                 new SqlParameter("@Hcompany", hv.Hcompany),
                                                 new SqlParameter("@Hlogo", hv.Hlogo),
                                                 new SqlParameter("@Hpic", hv.Hpic), 
                                                 new SqlParameter("@Hmap", hv.Hmap), 
                                                 new SqlParameter("@Htype", hv.Htype),
                                                 new SqlParameter("@Hclass", hv.Hclass)};
        Exception ex = null;
        ex = dbm.Execute(sql, sp);
        return ex;
    }

    //查找用户控件最新楼盘信息
    public DataTable getHouse(string count)
    {
        sql = "select top " + count + " * from House order by Hid desc";
        dt = new DataTable();
        dt = dbm.GetTable(sql);
        return dt;
    }

    //随机显示首页楼盘信息
    public DataTable getIndexHouse(int count)
    {
        sql = "select top 1 Hid from House order by Hid desc";
        dt = new DataTable();
        dt = dbm.GetTable(sql);
        if (dt.Rows.Count > 0)
        {
            int maxvalue = Convert.ToInt32(dt.Rows[0][0]);
            if (maxvalue > count)
            {
                string SidList = "";
                Random random = new Random();
                ArrayList list = new ArrayList();
                for (int i = 0; i < count; i++)
                {
                    bool flag = true;
                    int id = random.Next(1, maxvalue + 1);
                    while (flag)
                    {
                        if (list.Contains(id.ToString()))
                        {
                            id = random.Next(1, maxvalue + 1);
                            flag = true;
                        }
                        else
                        {
                            list.Add(id.ToString());
                            flag = false;
                        }
                    }
                    if (i == 0)
                    {
                        SidList = id.ToString();
                    }
                    else
                    {
                        SidList = SidList + "," + id.ToString();
                    }
                }
                sql = "select * from House where Hid in (" + SidList + ")";
            }
            else
            {
                sql = "select * from House order by Hid desc";
            }
            dt = new DataTable();
            dt = dbm.GetTable(sql);
            return dt;
        }
        else
        {
            return null;
        }
    }

    //查找相关楼盘信息
    public DataTable getHouse(string name, string moneymin,string moneymax,string type,string count)
    {
        if (count == "0")
        {
            if (type == "0")
            {
                sql = "select * from House where Hname like @name and Hcost>=@moneymin and Hcost<=@moneymax order by Hup desc";
            }
            else
            {
                sql = "select * from House where Hname like @name and Hcost>=@moneymin and Hcost<=@moneymax and Htype=@type order by Hup desc";
            }
        }
        else
        {
            if (type == "0")
            {
                sql = "select top " + count + " * from House where Hname like @name and Hcost>=@moneymin and Hcost<=@moneymax order by Hup desc";
            }
            else
            {
                sql = "select top " + count + " * from House where Hname like @name and Hcost>=@moneymin and Hcost<=@moneymax and Htype=@type order by Hup desc";
            }
        }
        SqlParameter[] sp = new SqlParameter[] { new SqlParameter("@name", "%" + name + "%"),
                                                 new SqlParameter("@moneymin", moneymin),
                                                 new SqlParameter("@moneymax", moneymax),
                                                 new SqlParameter("@type", type)};
        dt = new DataTable();
        dt = dbm.GetTable(sql, sp);
        return dt;
    }

    //查找相同类型楼盘信息
    public DataTable getHouse(string hid, string type)
    {

        sql = "select top 5 * from House where Hid<>@Hid and Htype=@Htype order by Hup desc";
        SqlParameter[] sp = new SqlParameter[] { new SqlParameter("@Hid", hid),
                                                 new SqlParameter("@Htype", type)};
        dt = new DataTable();
        dt = dbm.GetTable(sql, sp);
        return dt;
    }

    //获得单个楼盘信息
    public HouseVo getSingleHouse(string id)
    {
        sql = "select * from House where Hid=@Hid";
        SqlParameter[] sp = new SqlParameter[] { new SqlParameter("@Hid", id) };
        dt = new DataTable();
        dt = dbm.GetTable(sql, sp);
        hv = new HouseVo();
        hv.Hid = Convert.ToInt32(dt.Rows[0][0]);
        hv.Hname = dt.Rows[0][1].ToString();
        hv.Hintroduce = dt.Rows[0][2].ToString();
        hv.Hcontent = dt.Rows[0][3].ToString();
        hv.Haddress = dt.Rows[0][4].ToString();
        hv.Hcost = Convert.ToInt32(dt.Rows[0][5]);
        hv.Harea = dt.Rows[0][6].ToString();
        hv.Htime = dt.Rows[0][7].ToString();
        hv.Htel = dt.Rows[0][8].ToString();
        hv.Hperson = dt.Rows[0][9].ToString();
        hv.Hcenter = dt.Rows[0][10].ToString();
        hv.Hdeveloper = dt.Rows[0][11].ToString();
        hv.Hcompany = dt.Rows[0][12].ToString();
        hv.Hlogo = dt.Rows[0][13].ToString();
        hv.Hpic = dt.Rows[0][14].ToString();
        hv.Hmap = dt.Rows[0][15].ToString();
        hv.Htype = dt.Rows[0][16].ToString();
        hv.Hclass = dt.Rows[0][17].ToString();
        hv.Hup = Convert.ToInt32(dt.Rows[0][18]);
        hv.Hstate = Convert.ToInt32(dt.Rows[0][19]);
        return hv;
    }


    //修改相关楼盘信息
    public Exception updateSingleHouse(HouseVo hv)
    {
        sql = "update House set Hname=@Hname,Hintroduce=@Hintroduce,Hcontent=@Hcontent,Haddress=@Haddress,Hcost=@Hcost,Harea=@Harea,Htime=@Htime,Htel=@Htel,Hperson=@Hperson,Hcenter=@Hcenter,Hdeveloper=@Hdeveloper,Hcompany=@Hcompany,Hlogo=@Hlogo,Hpic=@Hpic,Htype=@Htype,Hclass=@Hclass where Hid=@Hid";
        SqlParameter[] sp = new SqlParameter[] { new SqlParameter("@Hname", hv.Hname), 
                                                 new SqlParameter("@Hintroduce", hv.Hintroduce), 
                                                 new SqlParameter("@Hcontent", hv.Hcontent), 
                                                 new SqlParameter("@Haddress", hv.Haddress), 
                                                 new SqlParameter("@Hcost", hv.Hcost),
                                                 new SqlParameter("@Harea", hv.Harea),
                                                 new SqlParameter("@Htime", hv.Htime),
                                                 new SqlParameter("@Htel", hv.Htel),
                                                 new SqlParameter("@Hperson", hv.Hperson),
                                                 new SqlParameter("@Hcenter", hv.Hcenter),
                                                 new SqlParameter("@Hdeveloper", hv.Hdeveloper),
                                                 new SqlParameter("@Hcompany", hv.Hcompany),
                                                 new SqlParameter("@Hlogo", hv.Hlogo),
                                                 new SqlParameter("@Hpic", hv.Hpic),
                                                 new SqlParameter("@Htype", hv.Htype),
                                                 new SqlParameter("@Hclass", hv.Hclass),
                                                 new SqlParameter("@Hid", hv.Hid)};
        Exception ex = null;
        ex = dbm.Execute(sql, sp);
        return ex;
    }

    //删除单个楼盘
    public Exception delHouse(string id)
    {
        sql = "delete House where Hid=@Hid";
        SqlParameter[] sp = new SqlParameter[] { new SqlParameter("@Hid", id) };
        Exception ex = null;
        ex = dbm.Execute(sql, sp);
        return ex;
    }

    //增加支持数
    public Exception HouseAddUp(string id)
    {
        sql = "update House set Hup=Hup+1 where Hid=@Hid";
        SqlParameter[] sp = new SqlParameter[] { new SqlParameter("@Hid", id) };
        Exception ex = null;
        ex = dbm.Execute(sql, sp);
        return ex;
    }
}
